import { h } from 'vue';
import { DescItem } from '/@/components/Description';
import { CodeEditor, JsonPreview, MODE } from '/@/components/CodeEditor';

const paramRender = () => (curVal) => {
  if (curVal) {
    const data: Object = JSON.parse(curVal);
    const keys: Array<string> = Object.keys(data);
    // 只有一个参数的情况
    if (keys.length === 1) {
      const param = data[keys[0]];
      const paramKeys: Array<string> = Object.keys(param);
      let result = {};
      paramKeys.forEach((key: string) => {
        if (param[key].detail) {
          if (param[key].type.startsWith('List')) {
            result[param[key].name] = [];
            let children = {};
            param[key].detail.forEach((child) => {
              children[child.name] = mockValue(child.type);
            });
            result[param[key].name].push(children);
          } else {
            param[key].detail.forEach((child) => {
              result[param[key].name][child.name] = mockValue(child.type);
            });
          }
        } else {
          result[param[key].name] = mockValue(param[key]);
        }
      })
      return h(CodeEditor, { value: JSON.stringify(result), mode: MODE.JSON })
    }
    // 多个参数的情况，分开处理是因为获取到的参数名称是错误的，而且绝大多数都是一个参数
    else {
      let result: object = {};
      keys.forEach((key: string) => {
        const param = data[key];
        const paramKeys: Array<string> = Object.keys(param);
        let item = {};
        paramKeys.forEach((key: string) => {
          if (param[key].detail) {
            if (param[key].type.startsWith('List')) {
              item[param[key].name] = [];
              let children = {};
              param[key].detail.forEach((child) => {
                children[child.name] = mockValue(child.type);
              });
              item[param[key].name].push(children);
            } else {
              param[key].detail.forEach((child) => {
                item[param[key].name][child.name] = mockValue(child.type);
              });
            }
          } else {
            item[param[key].name] = mockValue(param[key]);
          }
        });
        result[key] = item;
      });
      return h(CodeEditor, { value: JSON.stringify(result), mode: MODE.JSON });
    }
  } else {
    return curVal;
  }
}

// 模拟请求参数
const mockValue = (item) => {
  if (item.type == 'String') {
    return '';
  } else if (item.type == 'Integer') {
    return 1;
  } else if (item.type == 'LocalDate') {
    return '2024-01-01';
  } else if (item.type == 'LocalDateTime') {
    return '2024-01-01 09:30:12';
  } else if (item.type == 'Boolean') {
    return true;
  } else if (item.type == 'List<String>') {
    return ['1', '2'];
  } else if (item.type == 'List<Integer>') {
    return [1, 2];
  } else {
    return null;
  }
}

export const descItem: DescItem[] = [
  {
    field: 'name',
    label: '接口名称',
    span: 2,
  },
  {
    field: 'path',
    label: '请求路径',
    span: 2,
  },
  {
    field: 'requestParam',
    label: '请求参数',
    span: 2,
    labelMinWidth: 120,
    render: paramRender()
  },
  {
    field: 'responseData',
    label: '响应信息',
    span: 2,
    labelMinWidth: 120,
    // render: tableRender()
  },
];
